﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore;
using MvcMusicStore.Helpers;
using MvcMusicStore.Models;


namespace MvcMusicStore.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class SongManagerController : Controller
    {
        private MusicShopDBEntities db = new MusicShopDBEntities();
        private readonly List<string> acceptableContentTypes = new List<string> { "audio/mpeg", "audio/mp3" };

        //
        // GET: /SongManager/
        [HttpGet]
        public ViewResult Index()
        {
            
                var song = db.Song.Include("Album").Include("Author").Include("Genre");
                return View(song.ToList());
            
        }

        //
        // GET: /SongManager/Details
        [HttpGet]
        public ViewResult Details(int id)
        {
           
                Song song = db.Song.Single(s => s.id == id);
                return View(song);
            
        }

        //
        // GET: /SongManager/Create
        [HttpGet]
        public ActionResult Create()
        {
            
                ViewBag.idAlbum = new SelectList(db.Album, "id", "titleAlbum");
                ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor");
                ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre");
                return View();
            
        }

        //
        // POST: /SongManager/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Song song, HttpPostedFileBase file)
        {
            
                if (null != file && (0 < file.ContentLength) && acceptableContentTypes.Contains(file.ContentType))
                {
                    Guid guid = Guid.NewGuid();
                    string contentType = file.ContentType;
                    song.dateAdd = DateTime.Now;
                    song.counterBought = 0;

                    if (ModelState.IsValid)
                    {
                        try
                        {
                            db.Song.AddObject(song);
                            db.SaveChanges();
                            using (Stream clientStream = file.InputStream)
                            {
                                StorageHelper.UpdateSong(song.id, guid, contentType, clientStream);
                            }
                        }
                        catch
                        {
                            ViewBag.idAlbum = new SelectList(db.Album, "id", "titleAlbum", song.idAlbum);
                            ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", song.idAuthor);
                            ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", song.idGenre);

                            return View(song);
                        }
                    }
                }
                return RedirectToAction("Edit", new {id = song.id});
           
        }

        //
        // GET: /SongManager/Edit
        [HttpGet]
        public ActionResult Edit(int id)
        {
           
                Song song = db.Song.Single(s => s.id == id);
                ViewBag.idAlbum = new SelectList(db.Album, "id", "titleAlbum", song.idAlbum);
                ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", song.idAuthor);
                ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", song.idGenre);
                return View(song);
            
        }

        //
        // POST: /SongManager/Edit

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Song song, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.Song.Attach(song);
                    db.ObjectStateManager.ChangeObjectState(song, EntityState.Modified);
                    db.SaveChanges();
                }
                catch
                {
                }

                if (null != file && (0 < file.ContentLength) && acceptableContentTypes.Contains(file.ContentType))
                {
                    Guid guid = Guid.NewGuid();
                    string contentType = file.ContentType;

                    if (ModelState.IsValid)
                    {
                        try
                        {
                            using (Stream clientStream = file.InputStream)
                            {
                                StorageHelper.UpdateSong(song.id, guid, contentType, clientStream);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            ViewBag.idAlbum = new SelectList(db.Album, "id", "titleAlbum", song.idAlbum);
            ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", song.idAuthor);
            ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", song.idGenre);

            return View(song);
          
        }

        //
        // GET: /SongManager/Delete/
        [HttpGet]
        public ActionResult Delete(int id)
        {
            
                Song song = db.Song.Single(s => s.id == id);
                return View(song);
            
        }

        //
        // POST: /SongManager/Delete/


        [HttpPost]
        [ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
           
                Song song = db.Song.Single(s => s.id == id);
                if (song.counterBought == 0)
                {
                    db.Song.DeleteObject(song);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return View("NoDelete", song);
                }
           
        }

        //
        // GET: /SongManager/Player/
        [HttpGet]
        public ActionResult Player(int? id)
        {
            
                if (!id.HasValue) return HttpNotFound();
                ViewBag.PlayId = id.Value;
                return View();
            
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
